package com.ijiuchuang.jc_localization.rx;

import com.ijiuchuang.jc_localization.bean.event.BeaconTagData;
import com.ijiuchuang.jc_localization.tools.utils.HexUtil;

import rx.functions.Func1;

/**
 * Created by Veev on 2016/8/15
 *
 * @link 384506557@qq.com
 */
public class MapOnNewStringToBeaconTagData implements Func1<String, BeaconTagData> {

    private static volatile MapOnNewStringToBeaconTagData sInst = null;

    public static MapOnNewStringToBeaconTagData getInstance() {
        MapOnNewStringToBeaconTagData inst = sInst;
        if (inst == null) {
            synchronized (MapOnNewStringToBeaconTagData.class) {
                inst = sInst;
                if (inst == null) {
                    inst = new MapOnNewStringToBeaconTagData();
                    sInst = inst;
                }
            }
        }
        return inst;
    }

    private MapOnNewStringToBeaconTagData() {
    }

    @Override
    public BeaconTagData call(String str) {

        BeaconTagData beaconTagData = new BeaconTagData();

        beaconTagData.beaconUuid = str.substring(0, 4);
        beaconTagData.tagUuid = str.substring(4, 8);
        // rssi 字符串
        String rssiStr = str.substring(8,18);
        char rssiChars[] = rssiStr.toCharArray();
        byte bytes[] = HexUtil.decodeHex(rssiChars);

        beaconTagData.rssi = bytes[0];
        beaconTagData.battery = bytes[1];
        beaconTagData.heartRate = bytes[2];
        beaconTagData.alarm = bytes[3];
        beaconTagData.packetFlags = bytes[4];

        return beaconTagData;
    }
}
